package core

import grails.converters.XML
import grails.converters.deep.JSON

class CountryController {
	
	
	static layout = 'country'
	

	def create() {
	}
	def editCoun()
	{
		[country: Country.get(params.long('id'))]	
	}
	
	
	def deleteCoun()
	{
	def country =  Country.get(params.id)
	   
		country.delete()
		redirect(action:'index')
	}
	

	def update() {
		def country =  Country.get(params.long('id'))
		country.properties = params
		if(country.save()) {
			flash.notice = "Country: ${country.code} was updated"
			redirect action:'index'
		}
		else {
			flash.error = "Country:  failed to be update"
			render(view:'editCoun',model:[country: country])
		}
	}
	def save() {
		def country = new Country(params)
		if (country.save()) {
			flash.notice = "Country ${country.code}: is saved successfuly"
			redirect(action:'index')
		} else {
			flash.error = "Country ${country.code}: Failed to save"
			render view:'create', model:[country:country]
		}
	}

	def index() {
		//		def imageUrl = createLinkTo(dir:"images", file:"springsource.png").toString()
		//		def imageUrl1 = g.createLinkTo(dir:"images", file:"springsource.png").toString()
		//		println imageUrl

		def max = params.long('max')?:20
		def start = params.long('start')?:0
		[countries:Country.list(max: max, offset:start ),max:max, start:start  ]
	}
//	def rend () {
//		def countryList = Country.list()
//
//
//		render with status code
//		render(status: 503, text: 'Failed to render')
//		render countryList as JSON
//		render countryList as XML
//	}

	def countries() {
		def map = [ countries: Country.list() ]
		render(view: "show", model: map)
	}

	def show() {

		[country: Country.get(params.long('id'))]
	}
	def blog()
	{
		render(params)
		
	}
}

